const http = require('http')
const db = require('./db')
const dayjs = require('dayjs')

const server = http.createServer()
server.listen(8848,() => {
    console.log('服务器启动了','http://192.168.13.116:8848');
})
server.on('request',(requset,response) => {
    const {url,method} = requset
    let data ={}
    if (method.toLowerCase() === 'get') {
        if (url === '/api/news') {
            data = db.news()
            data.forEach(el => {
                el.publish_time = dayjs(el.publish_time).format('YYYY-MM-DD HH:mm:ss')
            })
        } 
    }
    response.setHeader('Content-Type','application/json;charset = utf-8')
    response.setHeader('Access-Control-Allow-Origin','*')
   response.end(JSON.stringify(data))
})
